package com.jacen.etl.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class CollectUtils
{
  public static <T> Set<T> intersect(Collection<T> colLeft, Collection<T> colRight)
  {
    Set<T> set = new HashSet<T>();

    Map<Object, T> idMapLeft = getIdMap(colLeft);
    Map<Object, T> idMapRight = getIdMap(colRight);

    Iterator<Map.Entry<Object, T>> iter = idMapLeft.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<Object, T> entry = (Map.Entry<Object, T>)iter.next();
      if (!idMapRight.keySet().contains(entry.getKey())) continue; set.add(entry.getValue());
    }

    return set;
  }

  public static <T> Set<T> union(Collection<T> colLeft, Collection<T> colRight)
  {
    Set<T> set = new HashSet<T>();

    Map<Object, T> idMapLeft = getIdMap(colLeft);
    Set<Object> idSet = idMapLeft.keySet();
    set.addAll(idMapLeft.values());

    Map<Object, T> idMapRight = getIdMap(colRight);
    Iterator<Map.Entry<Object, T>> iter = idMapRight.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<Object, T> entry = (Map.Entry<Object, T>)iter.next();
      if (idSet.contains(entry.getKey()))
        continue;
      idSet.add(entry.getKey());
      set.add(entry.getValue());
    }

    return set;
  }

  public static <T> Set<T> diff(Collection<T> colLeft, Collection<T> colRight)
  {
    Set<T> set = new HashSet<T>();

    Map<Object, T> idMapLeft = getIdMap(colLeft);
    Map<Object, T> idMapRight = getIdMap(colRight);

    Set<Object> idSet = idMapRight.keySet();

    Iterator<Map.Entry<Object, T>> iter = idMapLeft.entrySet().iterator();
    while (iter.hasNext()) {
    	Map.Entry<Object, T> entry = (Map.Entry<Object, T>)iter.next();
        if (idSet.contains(entry.getKey())) continue; 
        set.add(entry.getValue());
    }

    return set;
  }

  public static Set<String> diffStr(Collection<String> colLeft, Collection<String> colRight)
  {
	  Set<String> set = new HashSet<String>();
    for (String str : colLeft) {
      if (colRight.contains(str)) continue; set.add(str);
    }
    return set;
  }

  public static Set<Integer> diffInt(Collection<Integer> colLeft, Collection<Integer> colRight)
  {
	  Set<Integer> set = new HashSet<Integer>();
    for (Integer i : colLeft) {
      if (colRight.contains(i)) continue; set.add(i);
    }
    return set;
  }

  public static <T> Map<Object, T> getIdMap(Collection<T> collect)
  {
    Map<Object, T> idMap = new HashMap<Object, T>();
    Iterator<T> iter = collect.iterator();
    while (iter.hasNext()) {
      T t = iter.next();
      Object id = BeanUtils.getProperty(t, "id");
      idMap.put(id, t);
    }
    return idMap;
  }

  public static <T> Set<Object> getIdSet(Collection<T> collect)
  {
    Set<Object> set = new HashSet<Object>();
    Iterator<T> iter = collect.iterator();
    while (iter.hasNext()) {
      Object t = iter.next();
      Object id = BeanUtils.getProperty(t, "id");
      set.add(id);
    }
    return set;
  }
}
